import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { MdlSnackbarService, MdlDialogService } from '@angular-mdl/core';
import { ReportConfigService } from '../report-config.service'
import { DialogService } from 'primeng/api';
import { ReportConfigFieldAddComponent } from '../base/report-config-fieldadd.component';

@Component({
    selector: 'report-config-cond',
    templateUrl: './report-config-cond.component.html',
    styleUrls: ['./report-config-cond.component.scss']
})
export class ReportConfigCondComponent implements OnInit {
    currentReportId: number = 0;
    condTypes: any = [{ label: '文本控件', value: 1 }, { label: '日期控件', value: 2 }, { label: '选择控件', value: 3 }]
    cols: any[] = [
        { header: '显示名称', field: 'fieldDisplayName', style: { 'text-align': 'center', 'overflow': 'hidden', 'width': '15%' } },
        { header: '条件类型', field: 'condTypeId', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } },
        { header: '必填标识', field: 'requireFlag', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } },
        { header: '选择字典', field: 'checkDict', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } },
        { header: 'SQL扩展', field: 'extendSql', style: { 'text-align': 'center', 'overflow': 'hidden', 'padding': 0 } }
    ];
    conds: any[] = [
    ];
    @Input()
    set reportId(report: number) {
        if (report) {
            this.currentReportId = report;
            this.reportConfigService.loadReportSearchCond(this.currentReportId).subscribe(data => this.conds = data);
        }
    }
    get reportId(): number { return this.currentReportId; }

    constructor(
        private fb: FormBuilder,
        private reportConfigService: ReportConfigService,
        private dialogService: DialogService,
        private mdlDialogService: MdlDialogService,
        private mdlSnackbarService: MdlSnackbarService) {
    }

    ngOnInit() {
    }

    /**添加查询条件 */
    addCond() {
        let ids = [];
        this.conds.forEach(e => ids.push(e['fieldId']));
        const ref = this.dialogService.open(ReportConfigFieldAddComponent, {
            'header': '添加查询条件',
            'width': '800px',
            contentStyle: { "height": "650px" },
            data: { reportId: this.currentReportId, checkedIds: ids }
        });
        ref.onClose.subscribe((data: any) => {
            if (data) {
                this.reportConfigService.createReportConds(data).subscribe(data => {
                    this.reportConfigService.loadReportSearchCond(this.currentReportId).subscribe(data => this.conds = data);
                });
            }
        });
    }

    /** 删除条件 */
    deleteCond(rowData) {
        this.mdlDialogService.confirm('您确定要删除条件吗？', "取消", "确定").subscribe(() => {
            this.reportConfigService.deleteReportCond(rowData.condId).subscribe(data => {
                this.reportConfigService.loadReportSearchCond(this.currentReportId).subscribe(data => this.conds = data);
            });
        });
    }

    /** 保存条件 */
    save() {
        this.reportConfigService.updateReportConds(this.conds).subscribe(data=>this.mdlSnackbarService.showToast('保存成功'));
    }
}
